package com.hwhhhh.graduationproject.controller;

import com.hwhhhh.graduationproject.annotation.CurrentUser;
import com.hwhhhh.graduationproject.domain.model.FreeClassroom;
import com.hwhhhh.graduationproject.domain.model.User;
import com.hwhhhh.graduationproject.exception.NoFreeClassroomException;
import com.hwhhhh.graduationproject.utils.ResponseCode;
import com.hwhhhh.graduationproject.utils.ResponseData;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description detail
 * Created by Hwhhhh on 2021/3/2 15:11
 */
@RestController
public class ClassroomController extends BaseController {
    /**
     * 通过教室空闲情况获取教室
     * @param freeType
     * @return
     */
    @GetMapping("/classrooms")
    public ResponseData getFreeClassroomByFreeType(@RequestParam("freeType") int freeType) {
        try {
            List<FreeClassroom> freeClassroomList = this.classroomService.getFreeClassroomsByFreeType(freeType);
            return new ResponseData(ResponseCode.SUCCESS, freeClassroomList);
        } catch (NoFreeClassroomException e) {
            e.printStackTrace();
            return new ResponseData(ResponseCode.NO_FREECLASS); // 没有空闲教室
        }
    }

    /**
     * 通过教室编号模糊查询教室
     * @param key 教师编号关键字
     * @return  返回<id, classroomNum>
     */
    @GetMapping("/classrooms")
    public ResponseData findFreeClassroomByKey(@RequestParam("key") String key) {
        try {
            Map<Integer, String> classroom = this.classroomService.findByClassroomNumLike(key);
            return new ResponseData(ResponseCode.SUCCESS, classroom);
        } catch (NoFreeClassroomException e) {
            e.printStackTrace();
            return new ResponseData(ResponseCode.NO_FREECLASS);
        }
    }

    /**
     * 根据空闲教室id获取教师信息
     * @param id
     * @return
     */
    @GetMapping("/classrooms/{id}")
    public ResponseData getFreeClassroomById(@PathVariable("id") int id) {
        return new ResponseData(this.classroomService.getById(id));
    }

    /**
     * 更新教室占用人数
     * @param id
     * @return
     */
    @PutMapping("/classrooms/occupy/{classroomId}")
    public ResponseData occupySeat(@PathVariable("classroomId") int id, @RequestParam("op") int op) {
        this.classroomService.updateOccupyNum(id, op);
        return new ResponseData();
    }

    /**
     * 预定教室
     * @param id
     * @param user
     * @return  返回预定后的
     */
    @PostMapping("/classrooms/{classroomId}/appointment")
    public ResponseData makeAppointment(@PathVariable("classroomId") int id, @CurrentUser User user) {
        this.classroomService.makeAppointment(id, user.getId());
        return new ResponseData();
    }

    /**
     * 取消预定
     * @param id 预定单的id
     * @return
     */
    @PutMapping("/classrooms/appointment/{appointmentId}")
    public ResponseData cancelAppointment(@PathVariable("appointmentId") int id) {
        this.classroomService.cancelAppointment(id);
        return new ResponseData();
    }
}
